package com.bindapp.model;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.bindapp.R;
import com.bindapp.activity.WsxxActivity;
import com.bindapp.bean.RegBean;
import com.bindapp.bean.SendCode;
import com.bindapp.databinding.ActivityRegBinding;
import com.bindapp.http.MNetHttp;
import com.bindapp.listener.HttpListener;
import com.bindapp.listener.TimeCountListener;
import com.bindapp.util.CommonUtil;
import com.bindapp.util.PreferencesUtil;
import com.bindapp.util.Static;
import com.bindapp.util.TimeCount;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by tom055 on 2018/6/23.
 */

public class RegModel extends BaseModel {
    private ActivityRegBinding binding;
    private TimeCount timeCount;

    public RegModel(final Context context) {
        super(context);
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.activity_reg, null, false);
        binding.setClick(new ViewClick());
        timeCount = new TimeCount(30 * 1000, 1 * 1000, new TimeCountListener() {
            @Override
            public void onTick(long millisUntilFinished) {
                binding.sendSms.setText(millisUntilFinished / 1000 + "s");

            }

            @Override
            public void onFinish() {
                binding.sendSms.setText("获取验证码");
                binding.sendSms.setBackground(ContextCompat.getDrawable(context, R.drawable.blue_view_bg));
            }
        });
    }


    public class ViewClick {
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.backbtn:
                    ((Activity) context).finish();
                    break;
                case R.id.submit:
                    if (TextUtils.isEmpty(binding.tell.getText())) {
                        Static.showToast(context, "手机号不能为空");
                    } else if (!CommonUtil.isMobile(binding.tell.getText().toString())) {
                        Static.showToast(context, "请检查手机号");
                    } else if (TextUtils.isEmpty(binding.password.getText())) {
                        Static.showToast(context, "密码不能为空");
                    } else if (binding.password.getText().length() < 6 || binding.password.getText().length() > 16) {
                        Static.showToast(context, "密码长度在6-16位之间");
                    } else if (TextUtils.isEmpty(binding.codeTxt.getText())) {
                        Static.showToast(context, "验证码不能为空");
                    } else {
                        showPassDialog("正在注册中...");
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("PhoneNumber", binding.tell.getText().toString());
                        map.put("Password", binding.password.getText().toString());
                        map.put("PhoneCode", binding.codeTxt.getText().toString());
                        MNetHttp.getInstance(context).rxPostRequest(Static.RegUrl, map, RegBean.class, new HttpListener() {
                            @Override
                            public void success(Object obj) {
                                dissPassDialog();
                                if (((RegBean) obj).isIsSuccess()&&((RegBean) obj).isBl()) {
                                    Static.showToast(context, "注册成功");
                                    PreferencesUtil.getInstance(context).setPhonenum(binding.tell.getText().toString());
                                    turnToVc(WsxxActivity.class, new HashMap<String, String>());
                                    ((Activity) context).finish();
                                } else {
                                    Static.showToast(context, ((RegBean) obj).getMsg());
                                }


                            }

                            @Override
                            public void fail(String msg) {
                                dissPassDialog();
                                Static.showToast(context, msg);
                            }
                        });
                    }
                    break;
                case R.id.sendSms:
                    if (TextUtils.isEmpty(binding.tell.getText())) {
                        Static.showToast(context, "手机号不能为空");
                    } else if (!CommonUtil.isMobile(binding.tell.getText().toString())) {
                        Static.showToast(context, "请检查手机号");
                    } else {
                        showPassDialog("正在发送");
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("PhoneNumber", binding.tell.getText().toString());
                        MNetHttp.getInstance(context).rxPostRequest(Static.SendCode, params, SendCode.class, new HttpListener() {
                            @Override
                            public void success(Object obj) {
                                dissPassDialog();
                                if (((SendCode) obj).getStatus() == 200) {
                                    Static.showToast(context, "验证码发送成功");
                                    binding.sendSms.setBackground(ContextCompat.getDrawable(context, R.drawable.gray_view_bg));
                                    //((SendCode)obj)
                                    timeCount.start();
                                } else {
                                    Static.showToast(context, "验证码发送失败，请重试");
                                }

                            }

                            @Override
                            public void fail(String msg) {
                                Static.showToast(context, msg);
                                dissPassDialog();
                            }
                        });
                    }


                    break;
            }

        }
    }

    @Override
    public ActivityRegBinding getBinding() {
        return binding;
    }
}
